#include "loginDB.h"
#include <QDebug>
#include <QFile>

LoginDB::LoginDB(QObject *parent) : QObject(parent)
{
    db = QSqlDatabase::addDatabase("QSQLITE");
}

bool LoginDB::createLoginDB(const QString dbPath)
{
    if(dbPath.isEmpty()){
        return false;
    }
    dbName = dbPath;
    db.setDatabaseName(dbName);
    QFile dbFile(dbPath);

    //若数据库存在，则打开数据库
    if(dbFile.exists()){
        if(!db.open()){
            qDebug() << "open user login db failed.";
            return false;
        }
        initModel();
        return true;
    }

    //若数据库文件不存在，则初始化数据库
    if(!db.open()){
        qDebug() << "open user login db failed.";
        return false;
    }
    QSqlQuery query(db);
    query.exec(QObject::tr("create table user (id INTEGER PRIMARY KEY AUTOINCREMENT, name vchar,passwd vchar, level int)"));
    query.exec(QObject::tr("insert into user values (NULL,'jyuser1','999',2)"));
    query.exec(QObject::tr("insert into user values (NULL,'jyuser2','888',2)"));
    query.exec(QObject::tr("insert into user values (NULL,'admin','666',0)"));

    initModel();
    return true;
}

bool LoginDB::updateUserInfo(const QString &name, const QString &passwd)
{
    QSqlQuery query(db);
    QString sql = QString ("update user set passwd = '%1' where name = '%2'").arg(passwd).arg(name);
    bool result = query.exec(sql);
    return result;
}

void LoginDB::initModel()
{
    model = new QSqlQueryModel();
    model->setQuery("select * from user");
    model->setHeaderData(0,Qt::Horizontal,QString("序号"));
    model->setHeaderData(1,Qt::Horizontal,QString("用户名"));
    model->setHeaderData(2,Qt::Horizontal,QString("密码"));
    model->setHeaderData(3,Qt::Horizontal,QString("用户角色"));
}

void LoginDB::refreshModel()
{
    model->setQuery("select * from user");
}

void LoginDB::deleteUser(const int row)
{
    int id = model->data(model->index(row,0)).toInt();
    QString sql = QString("delete from user where id =%1").arg(id);
    model->setQuery(sql);
    refreshModel();
}

void LoginDB::addUser(LoginUser *user)
{
    if(!user){
        return;
    }

    QString sql = QString("insert into user values (NULL,'%1','%2',%3)")
            .arg(user->getName())
            .arg(user->getPasswd())
            .arg(user->getLevel());

    qDebug() << sql;

    model->setQuery(sql);
    refreshModel();
}

void LoginDB::updateUser(LoginUser *user,int row)
{
    if(!user){
        return;
    }

    int id = model->data(model->index(row,0)).toInt();
    QString sql = QString("update user set name='%1',passwd='%2',level=%3 where id=%4")
            .arg(user->getName())
            .arg(user->getPasswd())
            .arg(user->getLevel())
            .arg(id);

    qDebug() << sql;

    model->setQuery(sql);
    refreshModel();
}


QStringList LoginDB::getUsersList()
{
    QSqlQuery query(db);
    query.exec("select * from user");
    usersList.clear();
    while (query.next())
    {
        QString userName = query.value(1).toString();
        usersList.append(userName);
    }
    return usersList;
}

QSqlQueryModel *LoginDB::getModel() const
{
    return model;
}

void LoginDB::getUserById(LoginUser *user, int row)
{
    if(!user){
        return;
    }

    QString name = model->data(model->index(row,1)).toString();
    QString passwd = model->data(model->index(row,2)).toString();
    int level = model->data(model->index(row,2)).toInt();

    user->setName(name);
    user->setPasswd(passwd);
    user->setLevel(level);
}
